#include "camerahelper.h"
#include <future>
#include <QDebug>


/// 处理掉线问题
void CameraHelper::handlerDis()
{
    currentStatus.initFailed(MU_DEVICE_CAM);
    TaskManager::GetInstance()->setATask(MU_TASK_INDEX_CAM,false);
    pushExp(MU_Messages::Camera,MU_EXPS::EXP_CAMERA_DIS,MU_ERROR_MODULE_CAM,DateTime::getFullTime());
}

void CameraHelper::Connect()
{
    if(currentStatus.GetInit(MU_DEVICE_CAM)){     /// 如果已经初始化了
        qWarning()<<"相机已经初始化成功!"<<__FUNCTION__;
        return ;
    }
    currentStatus.setInitType(MU_DEVICE_CAM,MU_TY_RUNNING);
    std::future<MU_RETURNS> result = std::async(std::launch::async,&CameraHelper::_open,this);


    if(MU_RETURN_SUCCESS != result.get()){
        currentStatus.initFailed(MU_DEVICE_CAM);
        std::cout << "相机连接:" << MU_Get_Msg(result.get()) << ".\n";
        return ;
    }

    currentStatus.initSucess(MU_DEVICE_CAM);
}

MU_RETURNS CameraHelper::reConnect()
{
    CloseCamera();
    Connect();
    /// 需要睡眠是否
    if(!currentStatus.GetInit(MU_DEVICE_CAM)){
        return MU_RETURN_ERROR;
    }
    return MU_RETURN_SUCCESS;
}

MU_RETURNS CameraHelper::_open()
{
    try {
        /// todo
         // _camera_ptr->connectDevice();
        return  MU_RETURN_SUCCESS;
    } catch (...) {
        throw MU_EXPS::EXP_CAMERA_OPEN;
        return  MU_RETURN_ERROR;
    }
    return  MU_RETURN_SUCCESS;
}

MU_RETURNS CameraHelper::CloseCamera()
{
    try {
        /* todo */
        currentStatus.SetStop(MU_DEVICE_CAM);    /// 设置相机状态:停止
        return  MU_RETURN_SUCCESS;
    } catch (...) {
        throw MU_EXPS::EXP_CAMERA_CLOSE;
        return  MU_RETURN_ERROR;
    }
}



//CameraHelper::CameraHelper()
//{
//    // _camera_ptr.reset(new Camera);
//}

CameraHelper::CameraHelper():
    _camera_ptr(Singleton<Camera>::get())
{

}
